import {defineStore} from "pinia";
import {ref} from "vue";
import {hasPermission} from "@/utils/permission.ts";
import {
    projectCategory,
    projectCategoryCreate,
    projectCategoryDelete,
    projectCategoryUpdate,
    projectStatus, projectStatusCreate, projectStatusDelete, projectStatusUpdate
} from "@/services/api/project.ts";
import useMessage from "@/utils/useMessage.ts";
import {Modal} from "ant-design-vue";

export const useProjectStore = defineStore('project',()=>{
    const dataSource = ref<any[]>([])
    const categoryList = ref<Category[]>([])
    const statusList = ref<StatusType[]>([])
    const loading = ref<boolean>(false)
    const getList = async (force?:boolean)=>{
        if(!force && dataSource.value.length > 0){
             return
        }
        try {
            loading.value = true
        }catch{}
        finally {
            setTimeout(()=>loading.value = false,100)
        }
    }
    const create = async ()=>{
        if (!hasPermission('project:create'))  return false
    }
    const update = async ()=>{
        if (!hasPermission('project:update'))  return false
    }
    const deleteItem = async ()=>{
        if (!hasPermission('project:delete'))  return false
    }
    const getCategoryList = async (force?:boolean)=>{
        if (!force && categoryList.value.length > 0) return false
        try {
            loading.value = true
            const res:ApiResponse<Category[]> = await projectCategory()
            if (res.code === 200) {
                categoryList.value = res.data
            }
            if (res.code !== 200) {
                useMessage(res.code, res.message)
            }
        }catch {}
        finally {
            setTimeout(()=>loading.value = false,100)
        }
    }
    const createCategory = async (data:CategoryForm)=>{
        if(!hasPermission('admin:project:create')) return false
        const res = await projectCategoryCreate(data)
        if (res.code === 200) {
            await getCategoryList(true)
        }
        useMessage(res.code, res.message)
        return res.code === 200
    }
    const updateCategory = async (data:CategoryForm)=>{
        if(!hasPermission('admin:project:update')) return false
        const res = await projectCategoryUpdate(data.id as number,data)
        if (res.code === 200) {
            const cItem = categoryList.value.find(item=>item.id === data.id)
            if (cItem){
                Object.assign(cItem,data)
            }
        }
        useMessage(res.code, res.message)
        return res.code === 200
    }
    const deleteCategory = async (id:number)=>{
        if(!hasPermission('admin:project:delete')) return false
        Modal.confirm({
            title: '删除项目分类',
            content: '确定要删除此项目分类吗？',
            onOk: async ()=>{
                const res = await projectCategoryDelete(id)
                if (res.code === 200) {
                    categoryList.value = categoryList.value.filter(item=>item.id !== id)
                }
                useMessage(res.code, res.message)
                return res.code === 200
            }
        })
    }
    const getStatusList = async (force?:boolean)=>{
        if (!force && statusList.value.length > 0) return
        try {
            loading.value = true
            const res:ApiResponse<StatusType[]> = await projectStatus()
            if (res.code === 200) {
                statusList.value = res.data
            }
            if (res.code !== 200) {
                useMessage(res.code, res.message)
            }
        }catch{}
        finally {
            setTimeout(()=>loading.value = false,100)
        }

    }
    const createStatus = async (data:StatusFormType)=>{
        if(!hasPermission('admin:project:create')) return false
        const res = await projectStatusCreate(data)
        if (res.code === 200) {
            await getStatusList(true)
        }
        useMessage(res.code, res.message)
        return res.code === 200
    }
    const updateStatus = async (data:StatusFormType)=>{
        if(!hasPermission('admin:project:update')) return false
        const res = await projectStatusUpdate(data.id as number,data)
        if (res.code === 200) {
            const cItem = statusList.value.find(item=>item.id === data.id)
            if (cItem){
                Object.assign(cItem,data)
            }
        }
        useMessage(res.code, res.message)
        return res.code === 200
    }
    const deleteStatus = async (id:number)=>{
        if(!hasPermission('admin:project:delete')) return false
        Modal.confirm({
            title: '删除项目状态',
            content: '确定要删除此项目状态吗？',
            onOk: async ()=>{
                const res = await projectStatusDelete(id)
                if (res.code === 200) {
                    statusList.value = statusList.value.filter(item=>item.id !== id)
                }
                useMessage(res.code, res.message)
                return res.code === 200
            }
        })
    }
    return {
        dataSource,
        categoryList,
        statusList,
        loading,
        getList,
        create,
        update,
        deleteItem,
        getCategoryList,
        createCategory,
        updateCategory,
        deleteCategory,
        getStatusList,
        createStatus,
        updateStatus,
        deleteStatus
    }
})